package net.gz01.wallet.web.controller.admin;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.AdminUser;
import net.gz01.wallet.web.model.Notice;

public class NoticeController extends BaseController {

	public void search() {
		List<Notice> list = Notice.dao.selectNoticeType();
		setAttr("noticetype",list);
		render("notice/noticeList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<Notice> pageResult = Notice.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void view() {
		Notice notice = Notice.dao.findById(getPara("id"));
		setAttr("noticeinfo", notice);
		render("notice/noticeView.html");
	}

	public void addPage(){
		List<Notice> list = Notice.dao.selectNoticeType();
		setAttr("noticetype",list);
		render("notice/noticeAdd.html");
	}

	public void add(){
		AdminUser user = (AdminUser)getSession().getAttribute("loginAdminUser");
		Notice notice = getModel(Notice.class);
		notice.setContent(getPara("content"));
		notice.setCreatetime(ZODate.getDateTime());
		notice.setUpdatetime(ZODate.getDateTime());
		notice.setEditor(user.getName());
		notice.save();
		renderSuccess();

	}

	public void update(){
		Notice notice = getModel(Notice.class);
		notice.setContent(getPara("content"));
		notice.setUpdatetime(ZODate.getDateTime());
		AdminUser user = (AdminUser)getSession().getAttribute("loginAdminUser");
		notice.setEditor(user.getName());
		notice.update();
		renderSuccess();
	}

	public void updatePage(){
		Notice notice = Notice.dao.findById(getPara("id"));
		List<Notice> list = Notice.dao.selectNoticeType();
		setAttr("noticetype",list);
		setAttr("noticeinfo", notice);
		render("notice/noticeUpdate.html");
	}

	@Before(Tx.class)
	public void delete(){
		Notice.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}

}
